# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        # 1. 特殊情况：链表为空
        if not head:
            return False

        # 2. 双指针
        slow, fast = head, head

        # 3. 算法流程：找到相遇点
        while True:

            if not fast or not fast.next:
                return False

            slow = slow.next
            fast = fast.next.next

            # 相遇
            if fast == slow:
                return True